package com.palm.hubo2o.ui;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.palm.hubo2o.BaseActivity;
import com.palm.hubo2o.R;
import com.palm.hubo2o.model.LoginData;
import com.palm.hubo2o.model.Password;
import com.palm.hubo2o.model.User;
import com.palm.hubo2o.network.PalmCallBack;
import com.palm.hubo2o.network.PalmRetrofit;
import com.palm.hubo2o.network.PalmSession;
import com.palm.hubo2o.network.Request;
import com.palm.hubo2o.network.Result;
import com.palm.hubo2o.util.PromptUtil;
import com.palm.hubo2o.view.ProgressDialogCompat;
import com.palm.palmutils.MD5;
import com.palm.palmutils.Settings;
import com.palm.palmutils.StringUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;

public class ModifyPwdActivity extends BaseActivity {

    @Bind(R.id.old_password)
    EditText oldPasswordEt;
    @Bind(R.id.new_password)
    EditText newPasswordEt;
    @Bind(R.id.new_password_repeat)
    EditText newPasswordRepeatEt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_pwd);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.finish_button)
    public void OnClick(View v){
        switch (v.getId()){
            case R.id.finish_button:
                if(oldPasswordEt==null||newPasswordEt==null||newPasswordRepeatEt==null){
                    return;
                }
                String oldPwd = oldPasswordEt.getText().toString();
                String newPwd = newPasswordEt.getText().toString();
                String newPwdRepeat = newPasswordRepeatEt.getText().toString();
                if(StringUtils.isEmpty(oldPwd)){
                    oldPasswordEt.setError(getString(R.string.error_password_null));
                }else if(oldPwd.length()<6||oldPwd.length()>15){
                    oldPasswordEt.setError(getString(R.string.error_password_size));
                    return;
                }else if(StringUtils.isEmpty(newPwd)){
                    newPasswordEt.setError(getString(R.string.error_password_null));
                }else if(newPwd.length()<6||newPwd.length()>15){
                    newPasswordEt.setError(getString(R.string.error_password_size));
                }else if(StringUtils.isEmpty(newPwdRepeat)){
                    newPasswordRepeatEt.setError(getString(R.string.error_password_null));
                }else if(newPwdRepeat.length()<6||newPwdRepeat.length()>15){
                    newPasswordRepeatEt.setError(getString(R.string.error_password_size));
                }else if(!newPwdRepeat.equals(newPwd)){
                    newPasswordRepeatEt.setError(getString(R.string.error_password_repeat));
                }else {
                    commitToSever(oldPwd,newPwd);
                }
                break;
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (android.R.id.home == item.getItemId()) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void commitToSever(String oldPwd,String newPwd){
        final ProgressDialogCompat progress = new ProgressDialogCompat(this);
        progress.show();
        Password data = new Password();
        data.setOldPwd(MD5.md5(oldPwd).substring(0, 16).toUpperCase());
        data.setNewPwd(MD5.md5(newPwd).substring(0, 16).toUpperCase());
        progress.setMessage(getString(R.string.prompt_loading));
        Call<Result<Void>> call = PalmRetrofit.getInstance().getApi().updatePassword(new Request<>(data));
        call.enqueue(new PalmCallBack<Void>(this) {
            @Override
            public void succeed(Void v) {
                    if(!progress.isCanceled())progress.dismiss();
                    PromptUtil.showToast(ModifyPwdActivity.this,getString(R.string.prompt_modify_success));

//                    PalmSession.logout();
//                    PalmSession.login(ModifyPwdActivity.this);
                    finish();

            }

            @Override
            public void failed(String message) {
                if (!progress.isCanceled()) {
                    progress.dismiss();
                    Toast.makeText(getContext(), StringUtils.isEmpty(message) ? getString(R.string.prompt_modify_failed) : message, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
